<!DOCTYPE html>
<html>
<head>
    <title>Electromagnetic Force on a Loop</title>
</head>
<body>
    <canvas id="physicsCanvas" width="450" height="450" style="border:1px solid #d3d3d3;"></canvas>
    <script>
        const canvas = document.getElementById('physicsCanvas');
        const ctx = canvas.getContext('2d');

        // --- Parameters and coordinates ---
        const wireX = 100;
        const loopTopY = 100;
        const d = 60;
        const b = 100;
        const l = 150;
        
        const loopLeftX = wireX + d;
        const loopRightX = loopLeftX + b;
        const loopBottomY = loopTopY + l;

        // --- Styling ---
        ctx.strokeStyle = 'black';
        ctx.fillStyle = 'black';
        ctx.lineWidth = 2;
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';

        /**
         * Draws a filled arrowhead.
         * @param {number} x - The x-coordinate of the arrow's tip.
         * @param {number} y - The y-coordinate of the arrow's tip.
         * @param {number} angle - The angle of the arrow's direction in radians.
         * @param {number} size - The length of the arrowhead.
         */
        function drawArrowhead(x, y, angle, size) {
            ctx.save();
            ctx.translate(x, y);
            ctx.rotate(angle);
            ctx.beginPath();
            ctx.moveTo(0, 0);
            ctx.lineTo(-size, -size / 2.5);
            ctx.lineTo(-size, size / 2.5);
            ctx.closePath();
            ctx.fill();
            ctx.restore();
        }

        // --- Main Drawing Logic ---

        // 1. Draw the long straight wire (I1)
        // Dashed part
        ctx.save();
        ctx.setLineDash([6, 4]);
        ctx.beginPath();
        ctx.moveTo(wireX, 50);
        ctx.lineTo(wireX, 120);
        ctx.stroke();
        ctx.restore();
        
        // Solid part
        ctx.beginPath();
        ctx.moveTo(wireX, 120);
        ctx.lineTo(wireX, 350);
        ctx.stroke();
        
        // Arrow for I1
        drawArrowhead(wireX, 160, -Math.PI / 2, 12);
        
        // Label I1
        ctx.font = 'italic 24px Times New Roman';
        ctx.fillText('I', wireX - 30, 170);
        ctx.font = 'italic 16px Times New Roman';
        ctx.fillText('1', wireX - 22, 175);
        
        // 2. Draw the rectangular loop (I2)
        ctx.beginPath();
        ctx.rect(loopLeftX, loopTopY, b, l);
        ctx.stroke();
        
        // Arrows for I2
        drawArrowhead(loopLeftX, loopTopY + l / 2, -Math.PI / 2, 12); // Upward
        drawArrowhead(loopRightX, loopTopY + l / 2, Math.PI / 2, 12); // Downward
        
        // Label I2
        ctx.font = 'italic 24px Times New Roman';
        ctx.fillText('I', loopLeftX + 25, loopTopY + 30);
        ctx.font = 'italic 16px Times New Roman';
        ctx.fillText('2', loopLeftX + 33, loopTopY + 35);

        // 3. Draw dimension lines
        ctx.lineWidth = 1.5;
        const arrowSize = 8;
        const tickSize = 8;
        ctx.font = 'italic 20px Times New Roman';
        
        // Dimension 'd' (no ticks, as per original image)
        const d_y = loopTopY + 40;
        ctx.beginPath();
        ctx.moveTo(wireX, d_y);
        ctx.lineTo(loopLeftX, d_y);
        ctx.stroke();
        // Arrowheads point inwards
        drawArrowhead(wireX, d_y, 0, arrowSize);
        drawArrowhead(loopLeftX, d_y, Math.PI, arrowSize);
        ctx.fillText('d', wireX + d / 2, d_y - 15);

        // Dimension 'b'
        const b_y = loopBottomY + 30;
        ctx.beginPath();
        // Line
        ctx.moveTo(loopLeftX, b_y);
        ctx.lineTo(loopRightX, b_y);
        // Ticks
        ctx.moveTo(loopLeftX, b_y - tickSize / 2);
        ctx.lineTo(loopLeftX, b_y + tickSize / 2);
        ctx.moveTo(loopRightX, b_y - tickSize / 2);
        ctx.lineTo(loopRightX, b_y + tickSize / 2);
        ctx.stroke();
        // Arrowheads
        drawArrowhead(loopLeftX, b_y, 0, arrowSize);
        drawArrowhead(loopRightX, b_y, Math.PI, arrowSize);
        ctx.fillText('b', loopLeftX + b / 2, b_y - 15);

        // Dimension 'l'
        const l_x = loopRightX + 40;
        ctx.beginPath();
        // Line
        ctx.moveTo(l_x, loopTopY);
        ctx.lineTo(l_x, loopBottomY);
        // Ticks
        ctx.moveTo(l_x - tickSize / 2, loopTopY);
        ctx.lineTo(l_x + tickSize / 2, loopTopY);
        ctx.moveTo(l_x - tickSize / 2, loopBottomY);
        ctx.lineTo(l_x + tickSize / 2, loopBottomY);
        ctx.stroke();
        // Arrowheads
        drawArrowhead(l_x, loopTopY, Math.PI / 2, arrowSize); // Points down
        drawArrowhead(l_x, loopBottomY, -Math.PI / 2, arrowSize); // Points up
        ctx.fillText('l', l_x + 20, loopTopY + l / 2);

        // 4. Draw the caption text
        ctx.font = '24px KaiTi';
        ctx.fillStyle = 'black';
        ctx.fillText('题 2 图', 225, 400);

    </script>
</body>
</html>